<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="设备编号" prop="deviceCode">
          <el-input
            v-model="queryParams.deviceCode"
            placeholder="请输入设备编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input
            v-model="queryParams.deviceName"
            placeholder="请输入设备名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:device:add']"
          >新增
          </el-button>
        </el-col>
        <!--      <el-col :span="1.5">-->
        <!--        <el-button-->
        <!--          type="success"-->
        <!--          plain-->
        <!--          icon="el-icon-edit"-->
        <!--          size="mini"-->
        <!--          :disabled="single"-->
        <!--          @click="handleUpdate"-->
        <!--          v-hasPermi="['system:device:edit']"-->
        <!--        >修改-->
        <!--        </el-button>-->
        <!--      </el-col>-->
        <!--      <el-col :span="1.5">-->
        <!--        <el-button-->
        <!--          type="danger"-->
        <!--          plain-->
        <!--          icon="el-icon-delete"-->
        <!--          size="mini"-->
        <!--          :disabled="multiple"-->
        <!--          @click="handleDelete"-->
        <!--          v-hasPermi="['system:device:remove']"-->
        <!--        >删除-->
        <!--        </el-button>-->
        <!--      </el-col>-->
        <!--      <el-col :span="1.5">-->
        <!--        <el-button-->
        <!--          type="warning"-->
        <!--          plain-->
        <!--          icon="el-icon-download"-->
        <!--          size="mini"-->
        <!--          @click="handleExport"-->
        <!--          v-hasPermi="['system:device:export']"-->
        <!--        >导出-->
        <!--        </el-button>-->
        <!--      </el-col>-->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="序号" align="center" prop="id"/>
        <el-table-column label="设备编号" align="center" prop="deviceCode" width="150"/>
        <el-table-column label="设备类型" align="center" prop="deviceType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.device_type" :value="scope.row.deviceType"/>
          </template>
        </el-table-column>
        <el-table-column label="设备名称" align="center" prop="deviceName"/>
        <el-table-column label="设备状态" align="center" prop="deviceStatus">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.deviceStatus">有效</el-tag>
            <el-tag type="danger" v-else>无效</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="设备位置" align="center" prop="deviceLocation"/>
        <el-table-column label="充值次数" align="center" prop="rechargeCount"/>
        <el-table-column label="设备剩余金额" align="center" prop="deviceBalance" width="100"/>
        <el-table-column label="设备剩余量" align="center" prop="deviceSurplus" width="300"/>
        <el-table-column label="总量" align="center" prop="totle"/>
        <!--      <el-table-column label="设备ip" align="center" prop="deviIp" />-->
        <!--      <el-table-column label="设备用户名" align="center" prop="deviUsername" />-->
        <!--      <el-table-column label="设备密码" align="center" prop="deviPassword" />-->
        <!--      <el-table-column label="设备sdk端口号" align="center" prop="deviSdkPort" />-->
        <!--      <el-table-column label="设备出入类型:1进入闸机，2出去闸机" align="center" prop="deviAccessType" />-->
        <!--      <el-table-column label="常开时段" align="center" prop="deviOpenAlwaysTimeIndex" />-->
        <!--      <el-table-column label="常闭时段" align="center" prop="deviCloseAlwaysTimeIndex" />-->
        <el-table-column label="空间位置" align="center" prop="bsloId"/>
        <el-table-column label="设备其他信息" align="center" prop="deviOtherInfo"/>
        <el-table-column label="剩余量单位" align="center" prop="surplusUnit"/>
        <el-table-column label="水表阀门状态" align="center" prop="sluiceStatus">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.sluiceStatus == 0">开阀门</el-tag>
            <el-tag type="danger" v-else>关阀门</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="电表总状态" align="center" prop="electricityStatus">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.electricityStatus == 0">拉闸</el-tag>
            <el-tag type="danger" v-else>合闸</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="电表A路状态" align="center" prop="electricityAStatus">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.electricityAStatus == 0">拉闸</el-tag>
            <el-tag type="danger" v-else>合闸</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="电表B路状态" align="center" prop="electricityBStatus">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.electricityBStatus == 0">拉闸</el-tag>
            <el-tag type="danger" v-else>合闸</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center"
                         fixed="right"
                         class-name="small-padding fixed-width"
                         width="210"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:device:edit']"
            >
            </el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:device:remove']"
            >
            </el-button>

            <el-dropdown trigger="click" @command="handleCommand">
      <span class="el-dropdown-link">
          <el-button type="info" icon="el-icon-setting" circle>
          </el-button>
      </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item icon="el-icon-check" :command="beforeHandleCommand(scope.row,0)">关闭</el-dropdown-item>
                <el-dropdown-item icon="el-icon-circle-plus" :command="beforeHandleCommand(scope.row,1)">打开</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改物联设备对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="网关ID" prop="gatewayId">
            <el-select v-model="form.gatewayId" placeholder="请选择网关ID">
              <el-option
                v-for="dict in dict.type.gateway"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="设备编号" prop="deviceCode">
            <el-input v-model="form.deviceCode" placeholder="请输入设备编号"/>
          </el-form-item>
          <el-form-item label="设备类型" prop="deviceType">
            <el-select v-model="form.deviceType" placeholder="请选择设备类型 1-电表 2-水表 3-人脸闸机">
              <el-option
                v-for="dict in dict.type.device_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="设备名称" prop="deviceName">
            <el-input v-model="form.deviceName" placeholder="请输入设备名称"/>
          </el-form-item>
          <el-form-item label="设备位置" prop="deviceLocation">
            <el-input v-model="form.deviceLocation" placeholder="请输入设备位置"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
  import request from '@/utils/request'
import {listDevice, getDevice, delDevice, addDevice, updateDevice} from "@/api/system/device";

export default {
  name: "Device",
  dicts: ['gateway', 'device_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物联设备表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceCode: null,
        deviceName: null,
      },
      over:{
        deviceCode:'',
        gatewayId:1,
        electricityStatus:''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceCode: [
          {required: true, message: "设备编号不能为空", trigger: "blur"}
        ],
        deviceType: [
          {required: true, message: "设备类型 1-电表 2-水表 3-人脸闸机不能为空", trigger: "change"}
        ],
        deviceName: [
          {required: true, message: "设备名称不能为空", trigger: "blur"}
        ],
        deviceLocation: [
          {required: true, message: "设备位置不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    beforeHandleCommand(index, command) {   //index我这里是遍历的角标，即你需要传递的额外参数
      return {
        'index': index,
        'command': command
      }
    },
    handleCommand(command) {
      //获取设备编号
      this.over.deviceCode=command.index.deviceCode;
      //获取网关id
      this.over.gatewayId=command.index.gatewayId;
      //获取开闸，关闸 状态
      this.over.electricityStatus=command.command;
        console.log(this.over)
        console.log(command)
      request({
        url: '/system/device/handlerGateSwitch?deviceCode='+this.over.deviceCode+"&gatewayId="+this.over.gatewayId+"&electricityStatus="+this.over.electricityStatus,
        method: 'get'
      }).then(resp=>{
        this.$message.success();
      })
      // this.$message('click on item ' + command);
    },
    /** 查询物联设备列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        gatewayId: null,
        deviceCode: null,
        deviceType: null,
        deviceName: null,
        deviceStatus: "0",
        deviceLocation: null,
        rechargeCount: null,
        deviceBalance: null,
        deviceSurplus: null,
        totle: null,
        deviIp: null,
        deviUsername: null,
        deviPassword: null,
        deviSdkPort: null,
        deviAccessType: null,
        deviOpenAlwaysTimeIndex: null,
        deviCloseAlwaysTimeIndex: null,
        bsloId: null,
        deviOtherInfo: null,
        surplusUnit: null,
        sluiceStatus: "0",
        electricityStatus: "0",
        electricityAStatus: "0",
        electricityBStatus: "0",
        createTime: null,
        createBy: null,
        modifyTime: null,
        modifyBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物联设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDevice(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物联设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物联设备编号为"' + ids + '"的数据项？').then(function () {
        return delDevice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
